package com.yuanli.cosmeticmirror.model.impl;

import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.comenjoysoft.baselibrary.util.SPUtils;
import com.orhanobut.logger.Logger;
import com.rxjava.rxlife.BaseScope;
import com.rxjava.rxlife.RxLife;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.base.HttpApi;
import com.yuanli.cosmeticmirror.bean.AlipayBean;
import com.yuanli.cosmeticmirror.bean.BaseBean;
import com.yuanli.cosmeticmirror.bean.MemberBean;
import com.yuanli.cosmeticmirror.bean.MemberInfoBean;
import com.yuanli.cosmeticmirror.bean.TutorialBean;
import com.yuanli.cosmeticmirror.bean.videoBean;
import com.yuanli.cosmeticmirror.model.biz.IMemberBiz;

import java.util.List;

import rxhttp.wrapper.param.RxHttp;

public class MemberImpl extends BaseScope implements IMemberBiz {
    public MemberImpl(LifecycleOwner owner) {
        super(owner);
    }

    @Override
    public void getPrices(CallbackListener<List<MemberBean>> listener) {
        RxHttp.get(HttpApi.GET_VIP_PRICE)
                .add("AppName", "化妆镜")
                .asString()
                .as(RxLife.asOnMain(this))
                .subscribe(s -> {
                    Logger.d("getPrices: success json: " + s);
                    BaseBean<MemberBean> tutorialBean = JSON.parseObject(s, new TypeReference<BaseBean<MemberBean>>() {
                    });
                    if (tutorialBean != null && tutorialBean.getList() != null) {
                        listener.success(tutorialBean.getList());
                    } else {
                        listener.failed(null);
                    }

                }, throwable -> {
                    Logger.e("getPrices: failed: " + throwable.getMessage());
                    listener.failed(null);
                });
    }

    @Override
    public void getVipInfo(CallbackListener<MemberInfoBean> listener) {
        RxHttp.get(HttpApi.GET_VIP_INFO)
                .add("userPhone", SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT))
                .add("appName", "化妆镜")
                .asString()
                .as(RxLife.asOnMain(this))
                .subscribe(s -> {
                    Logger.d("getVipInfo: success json: " + s);
                    MemberInfoBean tutorialBean = JSON.parseObject(s, MemberInfoBean.class);
                    if (tutorialBean != null) {
                        listener.success(tutorialBean);
                    } else {
                        listener.failed(null);
                    }

                }, throwable -> {
                    Logger.e("getVipInfo: failed: " + throwable.getMessage());
                    listener.failed(null);
                });
    }

    @Override
    public void pay(String json, boolean isWechat, CallbackListener<AlipayBean> listener) {
        String api = "";
        if (isWechat) {
            api = HttpApi.POST_WECHAT_PAY;
        } else {
            api = HttpApi.POST_ALIPAY_PAY;
        }

        RxHttp.postJson(api).addAll(json)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" pay onSuccess: " + s);
                    AlipayBean response = JSON.parseObject(s, AlipayBean.class);
                    if (response.getCode() == 1) {
                        listener.success(response);
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    Logger.d(" pay onFailed: " + throwable.getMessage());
                    //请求失败
                    listener.failed(throwable.getMessage());
                });
    }


}
